package servlet;

import entity.Constants;
import entity.Pager;
import entity.Student;
import service.StudentService;
import service.serviceimpl.StudentServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class StudentServlet extends HttpServlet {
    private StudentService stuService = new StudentServiceImpl();

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        //获取request参数
        String stuName = request.getParameter("stuName");

        int gender = Constants.DEFAULT_GENDER;
        String genderStr = request.getParameter("gender");
        if(genderStr!=null&& !"".equals(genderStr)){
            gender = Integer.parseInt(genderStr);
        }
        int pageSize = Constants.DEFAULT_PAGESIZE;
        String pageSizeStr = request.getParameter("pageSize");
        if(pageSizeStr!=null&& !"".equals(pageSizeStr)){
            pageSize = Integer.parseInt(pageSizeStr);
        }
        int pageNum = Constants.DEFAULT_PAGENUM;
        String pageNumStr = request.getParameter("pageNum");
        if(pageNumStr!=null&& !"".equals(pageNumStr)){
            pageNum = Integer.parseInt(pageNumStr);
        }
        //组装查询条件
        Student stu = new Student();
        stu.setStuName(stuName);
        stu.setGender(gender);
        System.out.println("进入页面，gender值为："+gender);
        //调用service查询
        Pager<Student> result = stuService.findStudent(stu, pageNum, pageSize);
        //返回结果
        request.setAttribute("result", result);
        request.getRequestDispatcher("index.jsp").forward(request, response);
    }


    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}
